package com.xdwanj.retrofittest.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.google.gson.Gson;
import com.xdwanj.retrofittest.R;
import com.xdwanj.retrofittest.data.LoginResult;
import com.xdwanj.retrofittest.data.TokenUser;
import com.xdwanj.retrofittest.data.User;
import com.xdwanj.retrofittest.fetchr.FlickrFetcher;
import com.xdwanj.retrofittest.urlhttp.CallBackUtil;
import com.xdwanj.retrofittest.urlhttp.UrlHttpUtil;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextView center;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        center = (TextView) findViewById(R.id.center);

        TokenUser user = new TokenUser("xdwanj", "123456");
        LiveData<LoginResult> data = login(user);
        data.observe(this, loginResult -> {
            Log.d(TAG, "onChanged: loginResult->" + loginResult);
        });

    }

    private void logout(String token) {
        String url = "http://124.93.196.45:10001/logout";
        UrlHttpUtil.post(url, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                Log.d(TAG, "onFailure: " + errorMessage);
            }

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "onResponse: " + response);
            }
        });
    }

    private LiveData<LoginResult> login(TokenUser user) {
        String url = "http://124.93.196.45:10001/prod-api/api/login";
        Gson gson = new Gson();
        String json = gson.toJson(user);
        Log.d(TAG, "onCreate: user->" + json);
        MutableLiveData<LoginResult> result = new MutableLiveData<>();

        UrlHttpUtil.postJson(url, json, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                Log.d(TAG, "onFailure: " + code + ", " + errorMessage);
            }

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "onResponse: " + response);
                result.setValue(gson.fromJson(response, LoginResult.class));
                Log.d(TAG, "onResponse: " + result);
            }
        });

        return result;
    }

    private void register() {
        String url = "http://124.93.196.45:10001/prod-api/api/register";
        Gson gson = new Gson();
        String user = gson.toJson(
                new User(
                        "",
                        "xdwanj",
                        "xdwanj",
                        "123456",
                        "13366668888",
                        "0",
                        "xdwanj@qq.com",
                        "666666666666666666"
                )
        );
        Log.d(TAG, "onCreate: user->" + user);

        UrlHttpUtil.postJson(url, user, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                Log.d(TAG, "onFailure: " + code + ", " + errorMessage);
            }

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "onResponse: " + response);
            }
        });
    }

}